﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using TomNet.AspNetCore.Mvc;
using TomNet.Demo.Entity.Entities;
using TomNet.Demo.Repository.Repositories;

namespace TomNet.Demo.Web.Controllers
{
    public class RepoController : ApiControllerBase
    {
        private readonly IFreeSql _fsql;
        private readonly IUserRepository _userRepository;
        private readonly IUserInfoRepository _userInfoRepository;

        public RepoController(IFreeSql fsql,
            IUserRepository userRepository,
            IUserInfoRepository userInfoRepository)
        {
            _fsql = fsql;
            _userRepository = userRepository;
            _userInfoRepository = userInfoRepository;
        }

        [HttpGet("baseop")]
        public IActionResult GetAsync()
        {
            #region 增加 ---- 所有方式全部提供批量以及异步操作方法
            // 测试数据
            var entity = new UserEntity
            {
                LoginName = "tomnet",
                Password = "123456"
            };

            // 基础用法
            int change = _fsql.Insert(entity).ExecuteAffrows();


            // freesql提供的仓储
            var repo = _fsql.GetRepository<UserEntity>();
            //entity = repo.Insert(entity);

            //// 自己写的仓储
            //entity = _userRepository.Insert(entity);

            #endregion

            #region 删除
            // 基础用法
            change = _fsql.Delete<UserEntity>().Where(a => a.Id == 1).ExecuteAffrows();

            // 仓储用法 (根据主键类型)
            _userRepository.Delete(1);
            _userRepository.Delete(m => m.Id == 1);

            // 其他用法 (此方法支持orm的级联删除)
            _fsql.Select<UserEntity>().Where(a => a.Id == 1).ToDelete().ExecuteAffrows();

            // 先查询，在删除
            entity = _userRepository.Where(m => m.Id == 1).ToOne();
            _userRepository.Delete(entity);
            #endregion

            #region 修改
            // 更新指定列 根据主键
            _fsql.Update<UserEntity>(1)
              .Set(a => a.LoginName, "newname")
              .ExecuteAffrows();

            // 更新指定列 根据条件
            _fsql.Update<UserEntity>()
              .Set(a => a.VersionRow + 1)
              .Set(a => a.Password == "123abc@")
              .Where(a => a.Id == 1)
              .ExecuteAffrows();

            // 仓储模式  先查在更新
            repo = _fsql.GetRepository<UserEntity>();
            // 此时快照 item
            var item = repo.Where(a => a.Id == 1).First();
            item.Password = "newpwd";
            // 对比快照时的变化
            repo.Update(item);

            // 仓储模式  不查询
            repo = _fsql.GetRepository<UserEntity>();
            item = new UserEntity { Id = 1 };
            // 此时快照 item
            repo.Attach(item);
            item.Password = "newpwd";
            // 对比快照时的变化
            repo.Update(item);

            // 仓储下的一个特殊形式
            repo.UpdateDiy
                .Set(m => new UserEntity { Password = "123456" })
                //.SetIf(1 == 1, m => m.Password, "123456")
                //.SetDto(new { password = "xxx" })
                .Where(m => m.Id == 1).ExecuteAffrows();

            // dto形式
            _fsql.Update<UserEntity>()
              .SetDto(new { password = "xxx", loginname = "tommy" })
              .Where(a => a.Id == 1)
              .ExecuteAffrows();

            _fsql.Update<UserEntity>()
              .SetDto(new Dictionary<string, object> { ["password"] = "xxx", ["loginname"] = "tommy" })
              .Where(a => a.Id == 1)
              .ExecuteAffrows();

            _fsql.Select<UserEntity>()
                .Where(m => m.Id == 1)
                .WhereIf("" == "", m => m.LoginName == "1")
                .ToList<UserDto>(m => new UserDto
                {
                    Name = m.LoginName
                });
            #endregion

            return Ok();
        }
    }

    public class UserDto
    {

        public int Id { get; set; }

        public string Name { get; set; }
    }
}
